package com.ljw.i18demo.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

/**
 * @Author LiuJunWen
 * @Date 2020/5/19 22:42
 */
@Component
@Slf4j
public class MessageUtils {
    private static MessageSource messageSource;

    public MessageUtils(MessageSource messageSource) {
        MessageUtils.messageSource = messageSource;
    }

    public static String getMessage(String key, Object... params) {
        try {
            return messageSource.getMessage(key, params, LocaleContextHolder.getLocale());
        } catch (Exception e) {
            log.error("[MessageUtils] getMessage failed...", e);
            return key;
        }
    }
}
